import React, { Component } from 'react';
import './index.less';
import { base, high } from 'nc-lightapp-front';
import getJobHistoryData from '../../JobHisModal/functions/getJobHistoryData';
import ReptObjModal from '../../ReptObjModal';
import getReptObjData from '../../ReptObjModal/functions/getReptObjData';
import getOnePsnData from '../../../container/functions/getOnePsnData';
import exportPsnPhoto from '../../../container/functions/exportPsnPhoto';
import templatePrint from '../functions/templatePrint';
import Uploader from '../../Uploader';
import printOutput from '../functions/printOutput';
import NCBackBtn from '../../../../../../../hrpub/common/components/hr-back';
//import viewCard from '../functions/viewCard';
import ViewCard from "src/hrpub/common/components/card-view";
import WpsModal from 'src/hrpub/common/components/wpsPreModel';
const { NCAffix } = base;
const { PrintOutput } = high;

class Header extends Component {
	constructor(props) {
		super(props);
		this.state = {
			reptObjVisible: false,
			reptColumns: [],
			reptData: [],
			showUploader: false,
			printData: {}
		};
		this.goBack = this.goBack.bind(this);
		this.onBtnOperation = this.onBtnOperation.bind(this);
		this.closeReptObjModal = this.closeReptObjModal.bind(this);
		this.pageQueryClick = this.pageQueryClick.bind(this);
	}

	componentDidMount() {
		this.props.cardPagination.getNextCardPaginationId({
			id: this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value,
			status: 1
		});
		this.props.cardPagination.setCardPaginationId({
			id: this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value,
			status: 1
		});
	}

	goBack() {
		this.props.goBack();
	}

	closeReptObjModal() {
		this.setState({
			reptObjVisible: false
		});
	}

	onBtnOperation(props, btncode, areacode, opt) {
		switch (btncode) {
			case 'JobHis':
				getJobHistoryData.call(
					this,
					this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value
				);
				break;
			case 'ReptObj':
				getReptObjData.call(
					this,
					this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value,
					(reptInfo) => {
						this.setState({
							reptColumns: reptInfo.columns,
							reptData: reptInfo.data,
							reptObjVisible: true
						});
					}
				);
				break;
			case 'Refresh':
				getOnePsnData.call(
					this,
					this.props.orgVal,
					this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value,
					(data) => {
						this.props.resetData(data);
					}
				);
				break;
			case 'cardrpt':
				//viewCard.call(this, this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psndoc.value);
				// this.detailViewCardNode.openCardViewModal(this.props.detailData.psnjobForm.hi_psnjob_h.rows);
				this.wpsview.openView(this.props.detailData.psnjobForm.hi_psnjob_h.rows)
				break;
			case 'ExportPhoto':
				exportPsnPhoto.call(this, this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value);
				break;
			case 'Print':
				templatePrint.call(this, this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value);
				break;
			case 'output':
				printOutput.call(this, this.props.detailData.psnjobForm.hi_psnjob_h.rows[0].values.pk_psnjob.value);
				break;
			case 'File':
				this.setState({
					showUploader: true
				});
				break;
			default:
				break;
		}
	}

	pageQueryClick(props, pk_psnjob) {
		getOnePsnData.call(this, this.props.orgVal, pk_psnjob, (data) => {
			this.props.resetData(data);
		});
	}

	render() {
		const { reptObjVisible, reptColumns, reptData, showUploader } = this.state;
		const { button, cardPagination,orgVal, detailData, table, json } = this.props;
		const { createCardPagination } = cardPagination;

		return (
			<div id="detail-header">
				<NCAffix container={document.getElementById('detail-header')}>
					<div className="header">
						<NCBackBtn onClick={this.goBack} />
						<div className="btn-group">
							{button.createButtonApp({
								area: 'head',
								onButtonClick: this.onBtnOperation
							})}
						</div>
						{createCardPagination({ handlePageInfoChange: this.pageQueryClick })}
					</div>
					<ReptObjModal
						json={json}
						visible={reptObjVisible}
						columns={reptColumns}
						data={reptData}
						onClose={this.closeReptObjModal}
					/>
					{showUploader && (
						<Uploader
                            json={json}
							table={table}
							onClose={() => {
								this.setState({
									showUploader: false
								});
							}}
							billId={detailData.psndocForm.bd_psndoc.rows[0].values.pk_psndoc.value}
						/>
					)}
					<PrintOutput
						ref="printOutput"
						url={'/nccloud/hrhi/psndoc/PsndocTemplatePrintAction.do'}
						data={this.state.printData}
					/>
				</NCAffix>
				<ViewCard
					{...this.props}
					onRef={
						node => this.detailViewCardNode = node
					}
				/>
				 <WpsModal
                        {...this.props}
                        onRef ={node => this.wpsview = node}
                        appcode={'60071030'}
                        pk_org={orgVal}
                    
                    />
			</div>
		);
	}
}

export default Header;
